package com.soft.javase.day11.copy;

import java.io.File;
import java.io.IOException;

/**
 * @author yzh
 * @desc TODO
 * @date 2025/5/9
 */
public class FolderCopy {
    public static void main(String[] args) {
        String sourceFolder = "source";
        String targetFolder = "target";

        try {
            copyFolder(sourceFolder, targetFolder);
            System.out.println("文件夹拷贝完成");
        } catch (IOException e) {
            System.err.println("文件夹拷贝失败: " + e.getMessage());
            e.printStackTrace();
        }
    }

    public static void copyFolder(String sourcePath, String targetPath) throws IOException {
        File sourceDir = new File(sourcePath);
        if (!sourceDir.exists()) {
            throw new IOException("源文件夹不存在: " + sourcePath);
        }

        File targetDir = new File(targetPath);
        if (!targetDir.exists() && !targetDir.mkdirs()) {
            throw new IOException("无法创建目标文件夹: " + targetPath);
        }

        File[] files = sourceDir.listFiles();
        if (files != null) {
            for (File file : files) {
                String newTargetPath = targetPath + File.separator + file.getName();

                if (file.isDirectory()) {
                    copyFolder(file.getAbsolutePath(), newTargetPath);
                } else {
                    FileCopy.copyFile(file.getAbsolutePath(), newTargetPath);
                }
            }
        }
    }
}
